import * as types from './mutation-types'

function switchColor(state, beforeColor, afterColor) {
  // 生产环境有BUG, 取消该功能
  if (true) {
    return;
  }

  state.color = afterColor;
  for (var i = 0; i < document.head.children.length; i++) {
    if (document.head.children[i].innerText.indexOf(beforeColor) >= 0) {
      var cssText = document.head.children[i].innerText;
      cssText = cssText.replace(
        new RegExp('(:|\\s+)' + beforeColor, 'ig'), '$1' + afterColor);

      document.head.children[i].innerText = cssText;
    }
  }
}

export default {
  [types.SWITCH_COLOR] (state, color) {
    if ( ! color) {
      return;
    }
    if (state.defaultColor != state.color) {
      switchColor(state, state.color, color);
    }
    switchColor(state, state.defaultColor, color);
    switchColor(state, state.hoverColor, color);
  },
  [types.RESET_COLOR] (state) {
    switchColor(state, state.color, state.defaultColor);
  },
};
